package com.imucs.campus_base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imucs.campus_base.entity.Course;
import com.imucs.campus_base.service.CourseService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping("/list")
    public Result list(){
        return new Result(true,StatusCode.OK,"查询成功",courseService.list());
    }

    @PostMapping("/selectBy")
    public Result selectBy(@RequestBody Map m){
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();

        if (null!=(String) m.get("id")){
            String id=(String) m.get("id");
            queryWrapper.eq("id",id);
        }
        if (null!=(String) m.get("title")){
            String title=(String) m.get("title");
            queryWrapper.eq("title",title);
        }
        List<Course> courseslist = courseService.list(queryWrapper);
        if (0!=courseslist.size()){
            return new Result(true,StatusCode.OK,"查询成功",courseslist);
        }else{
            return new Result(false,StatusCode.ERROR,"查询失败");
        }

    }

    @PostMapping("/deleteByid")
    public Result deleteById(@RequestBody Map m){
        String id = (String) m.get("id");
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);

        if (courseService.remove(queryWrapper)){
            return new Result(true,StatusCode.OK,"删除成功");
        }else{
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Map m){

        String id =(String) m.get("id");
        String title =(String) m.get("title");

        Course course = new Course();
        course.setId(id);
        course.setTitle(title);
        if (courseService.saveOrUpdate(course)) {
            return new Result(true,StatusCode.OK,"添加成功",course);
        }else {
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("update")
    public Result update(@RequestBody Map m){

        String id = (String) m.get("id");
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Course course = courseService.getOne(queryWrapper);

        if (null!=course){
            if (null!=(String)m.get("title")){
                course.setTitle((String)m.get("title"));
            }

            if (courseService.saveOrUpdate(course)) {
                return new Result(true,StatusCode.OK,"修改成功",course);
            }else{
                return new Result(false,StatusCode.ERROR,"数据没有成功保存");
            }

        }else{
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }

}
